import java.util.Arrays;

public class MyStack {
        public int[] elem;
        public int usedSize;

        public MyStack(){
            this.elem = new int[10];
        }

        public void push(int val){
            if(elem.length == usedSize){
                elem = Arrays.copyOf(elem,2*elem.length);
            }
            elem[usedSize] = val;
            usedSize++;
        }

        public int pop(){
            try {
                checkEmpty();
            }catch (StackIsEmptyException e){
                e.printStackTrace();
            }
            int oldVal = elem[usedSize - 1];
            usedSize--;
            return oldVal;
        }

        private void checkEmpty()throws StackIsEmptyException{
            if(usedSize == 0){
                throw new StackIsEmptyException("栈空间为空");
            }
        }

        public int peek(){
            if(empty()){
                return -1;
            }
            int oldVal = elem[usedSize - 1];
            return oldVal;
        }

        public boolean empty(){
            return elem.length == 0;
        }

}
